import { Controller, Get, Post, Body, Param, UseGuards, Request } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiBearerAuth, ApiBody } from '@nestjs/swagger';

import { UpdateUserInfoDto } from '../dto/update-userinfo.dto';
import { UpdatePasswordDto } from '../dto/update-password.dto';
import { UpdateAvatarDto } from '../dto/update-avatar.dto';
import { UserInfoService } from '../services/UserInfo.service';
import { JwtAuthGuard } from '@src/guard/auth.guard';


@Controller('userinfo')
@ApiTags('用户信息')
@ApiBearerAuth()
export class UserInfoController {
  constructor(private readonly userInfoService: UserInfoService) {}

  @Get()
  @UseGuards(JwtAuthGuard)
  @ApiOperation({ summary: '获取当前用户信息' })
  async getUserInfo(@Request() req: any) {
    const userId = req.user.id;
    return await this.userInfoService.getUserInfo(userId);
  }

  @Post()
  @UseGuards(JwtAuthGuard)
  @ApiOperation({ summary: '更新当前用户信息' })
  @ApiBody({ type: UpdateUserInfoDto, description: '新的用户信息' })
  async updateUserInfo(@Request() req: any, @Body() updateUserInfoDto: UpdateUserInfoDto) {
    const userId = req.user.id;
    console.log('updateUserInfoDto',updateUserInfoDto);
    
    return await this.userInfoService.updateUserInfo(userId, updateUserInfoDto);
  }

  @Post('updatepwd')
  @UseGuards(JwtAuthGuard)
  @ApiOperation({ summary: '更新当前用户密码' })
  @ApiBody({ type: UpdatePasswordDto, description: '新的密码信息' })
  async updatePassword(@Request() req: any, @Body() updatePasswordDto: UpdatePasswordDto) {
    const userId = req.user.id;
    return await this.userInfoService.updatePassword(userId, updatePasswordDto);
  }

  @Post('update/avatar')
  @UseGuards(JwtAuthGuard)
  @ApiOperation({ summary: '更新当前用户头像' })
  @ApiBody({ type: UpdateAvatarDto, description: '新的头像URL' })
  async updateAvatar(@Request() req: any, @Body() updateAvatarDto: UpdateAvatarDto) {
    const userId = req.user.id;
    return await this.userInfoService.updateAvatar(userId, updateAvatarDto);
  }

}
